const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // 设置视窗大小
  await page.setViewportSize({ width: 1920, height: 1080 });

  try {
    console.log('开始访问 http://47.115.205.12/BCAI/...');

    // 访问目标页面
    const response = await page.goto('http://47.115.205.12/BCAI/', {
      waitUntil: 'networkidle',
      timeout: 30000
    });

    console.log('响应状态:', response.status());
    console.log('响应URL:', response.url());

    // 等待几秒让页面加载
    await page.waitForTimeout(5000);

    // 获取页面标题
    const title = await page.title();
    console.log('页面标题:', title);

    // 检查是否有错误信息
    const errorElements = await page.$$eval('*', elements =>
      elements.filter(el => el.textContent && el.textContent.includes('404')).length
    );

    if (errorElements > 0) {
      console.log('发现404错误元素');
    }

    // 获取页面的HTML内容
    const htmlContent = await page.content();
    console.log('页面HTML长度:', htmlContent.length);

    // 检查是否有app元素
    const appElement = await page.$('#app');
    if (appElement) {
      const appContent = await appElement.innerHTML();
      console.log('App元素内容长度:', appContent.length);
      console.log('App元素内容预览:', appContent.substring(0, 200));
    } else {
      console.log('未找到app元素');
    }

    // 检查控制台错误
    page.on('console', (msg) => {
      console.log('控制台消息:', msg.type(), msg.text());
    });

    page.on('pageerror', (error) => {
      console.log('页面错误:', error.message);
    });

    page.on('requestfailed', (request) => {
      console.log('请求失败:', request.url(), request.failure().errorText);
    });

    // 截图
    await page.screenshot({ path: 'screenshot.png', fullPage: true });
    console.log('已保存截图到 screenshot.png');

    // 获取所有网络请求
    const requests = await page.evaluate(() => {
      return window.performance.getEntriesByType('resource').map(entry => ({
        name: entry.name,
        status: entry.responseStatus || 'N/A'
      }));
    });

    console.log('网络请求:');
    requests.forEach(req => {
      console.log(`  ${req.name} - 状态: ${req.status}`);
    });

  } catch (error) {
    console.error('访问页面时出错:', error.message);
  } finally {
    await browser.close();
  }
})();